home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4099 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.0 KB  |  81 lines

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: SORTing problem c++
  5. Date: 27 Jan 1996 23:58:27 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4eee73$504@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: pipe5.h1.usa.pipeline.com
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Jan 27, 1996 21:46:56 in article <SORTing problem c++>, 'heggie
  15. <heggie@pi.net>' wrote: 
  16.  
  17.  
  18. >I'm used to normal c 
  19. >just recent i switched to c++, now i am stucked 
  20. >with a program that did work in c but doesn't 
  21. >in c++. 
  22. >it is a program that sorts the 'argv'command line 
  23. >arguments array, 
  24. >what can I DO??? 
  25. >p.s. I am using borlands c++ 
  26. >p.s. thanks for reading this  
  27. >#include <stdio.h> 
  28. >#include <stdlib.h> 
  29. >#include <string.h> 
  30. >int sort_function(char **a, char **b) 
  31. >{ 
  32. >return( strcmp(*a, *b)); 
  33. >} 
  34. .. [ portions deleted ] ... 
  35. >qsort(argv, argc, sizeof(char*), sort_function); 
  36.  
  37. >****************************************** 
  38. >these are the error mesages, 
  39. >Error ..\SOURCES\OPDR_45.CPP 17: Cannot convert 'int (*)(char * *,char *  
  40. >*)' to 'int (*)(const void *,const void *)' 
  41. >Error ..\SOURCES\OPDR_45.CPP 17: Type mismatch in parameter '__fcmp' in  
  42. >call to 'qsort(void *,unsigned int,unsigned int,int (*)(const void  
  43. >*,const void *))' 
  44. Yup, your sort function needs to be rewritten.  Here's a sample 
  45. that I tested with MSVC++4.0, but should work on BC45 also: 
  46.  
  47. . your #includes plus 
  48. #include <iostream.h> 
  49.  
  50. int sort_function (const void * a, const void * b) 
  51.  { 
  52.    const char * A = *((const char **)a); 
  53.    const char * B = *((const char **)b); 
  54.    return strcmp(A, B); 
  55.  } 
  56.  
  57. int main () 
  58.  { 
  59.    char * bleep[7] = {"Now is", "The time", "For all", "Good men to", 
  60.                       "Come", "to the aid of", "their country."}; 
  61.    qsort(bleep, 7, sizeof(char*), sort_function); 
  62.    for (int i = 0; i < 7; i++) 
  63.      cout << bleep[i] << endl;  
  64.    return 0; 
  65.  } 
  66.  
  67. -- 
  68. Pete Grant 
  69. Kalevi, Inc. 
  70. Object Oriented Software Development
  71.